package com.smart.util;

public final class CastUtil {
    public static String castString(Object obj){
        return castString(obj,"");
    }
    public static String castString(Object obj,String value){
        return obj!=null?String.valueOf(obj):value;
    }
    public static double castDouble(Object obj){
        return CastUtil.castDouble(obj,0);
    }
    public static double castDouble(Object obj,double value){
        double doubleVal=value;
        if(obj!=null){
            String strVal=castString(obj);
            if(StringUtil.isNotEmpty(strVal)){
                try {
                    doubleVal=Double.parseDouble(strVal);
                } catch (NumberFormatException e) {
                    doubleVal=value;
                }
            }
        }
        return doubleVal;
    }
    public static double castLong(Object obj){
        return CastUtil.castLong(obj,0);
    }
    public static double castLong(Object obj,long value){
        long longVal=value;
        if(obj!=null){
            String strVal=castString(obj);
            if(StringUtil.isNotEmpty(strVal)){
                try {
                    longVal=Long.parseLong(strVal);
                } catch (NumberFormatException e) {
                    longVal=value;
                }
            }
        }
        return longVal;
    }
    public static int castInt(Object obj){
        return CastUtil.castInt(obj,0);
    }
    public static int castInt(Object obj,int value){
        int intVal=value;
        if(obj!=null){
            String strVal=castString(obj);
            if(StringUtil.isNotEmpty(strVal)){
                try {
                    intVal=Integer.parseInt(strVal);
                } catch (NumberFormatException e) {
                    intVal=value;
                }
            }
        }
        return intVal;
    }
    public static boolean castBoolean(Object obj){
        return CastUtil.castBoolean(obj,false);
    }
    public static boolean castBoolean(Object obj,boolean value){
        boolean booleanVal=value;
        if(obj!=null){
           booleanVal=Boolean.parseBoolean(castString(obj));
        }
        return booleanVal;
    }


}
